package com.itheima.controller;

import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pub.MessageConstant;
import com.itheima.service.CheckgroupService;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    RoleService roleService;

    // 分页查询角色
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map page = roleService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    // 添加或者更新角色
    @RequestMapping("addGroup")
    public Result addGroup(@RequestBody Role role,@RequestParam("checkitemIds") Integer[] checkitemIds){
        try {
            // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新
            roleService.addGroup(role,checkitemIds);
            return new Result(true, MessageConstant.UPDATE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("findCheckitemIds")
    public Result findCheckitemIds(@RequestParam("roleId") Integer roleId){
        try {
            // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新
            List<Integer> checkitemIds = roleService.findCheckitemIds(roleId);
            return new Result(true, "",checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"");
        }
    }

//
    // 根据id值删除检查项
    @RequestMapping("deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(@RequestParam("id") Integer id){
        try {
            Role role = new Role();
            role.setId(id);
            role.setIs_delete(1);
            // 目前来看此种方法最适合进行逻辑删除  值为null的不更新,  那为0或者0.0呢??(price变量 属性为float 为什么不更新)
            roleService.updateById(role);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
//
//    @RequestMapping("findAll")
//    public Result findAll(){
//        try {
//            List<Menu> list = roleService.findAll();
//            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
//        }
//    }
}
